<?php
// The general information at the top of each file
/**
* @version		$Id$
* @package		Joomla
* @copyright	Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
* @license		GNU General Public License, see LICENSE.php
*/
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport('joomla.plugin.plugin');
 
class plgContentNavegar extends JPlugin
{
	function onAfterDisplayContent( &$article, &$params, $limitstart )
	{
		global $mainframe;
		
		$view = JRequest::getVar('view');
		$Itemid = JRequest::getVar('Itemid');
		$byCategory = trim(JRequest::getVar('catid'));
		$institucional = JRequest::getInt('institucionales') == 1;
		
		$html = '';
		if ($view == "article") {
			$db 		= & JFactory::getDBO();
			$user		= & JFactory::getUser();
			$nullDate	= $db->getNullDate();

			$date		=& JFactory::getDate();
			$config 	= & JFactory::getConfig();
			$now 		= $date->toMySQL();

			$uid 		= $article->id;
			$option 	= 'com_content';
			$canPublish     = $user->authorize('com_content', 'publish', 'content', 'all');

			// the following is needed as different menu items types utilise a different param to control ordering
			// for Blogs the `orderby_sec` param is the order controlling param
			// for Table and List views it is the `orderby` param
			$params_list = $params->toArray();
			if (array_key_exists('orderby_sec', $params_list)) {
				$order_method = $params->get('orderby_sec', '');
			} else {
				$order_method = $params->get('orderby', '');
			}
			// additional check for invalid sort ordering
			if ( $order_method == 'front' ) {
				$order_method = '';
			}

			// Determine sort order
			switch ($order_method)
			{
				case 'date' :
					$orderby = 'a.created';
					break;

				case 'rdate' :
					$orderby = 'a.created DESC';
					break;

				case 'alpha' :
					$orderby = 'a.title';
					break;

				case 'ralpha' :
					$orderby = 'a.title DESC';
					break;

				case 'hits' :
					$orderby = 'a.hits';
					break;

				case 'rhits' :
					$orderby = 'a.hits DESC';
					break;

				case 'order' :
					$orderby = 'a.ordering';
					break;

				case 'author' :
					$orderby = 'a.created_by_alias, u.name';
					break;

				case 'rauthor' :
					$orderby = 'a.created_by_alias DESC, u.name DESC';
					break;

				case 'front' :
					$orderby = 'f.ordering';
					break;

				default :
					$orderby = 'a.ordering';
					break;
			}

			$xwhere = ' AND ( a.state = 1 OR a.state = -1 )' .
			' AND ( publish_up = '.$db->Quote($nullDate).' OR publish_up <= '.$db->Quote($now).' )' .
			' AND ( publish_down = '.$db->Quote($nullDate).' OR publish_down >= '.$db->Quote($now).' )';

			// array of articles in same category correctly ordered
			$query = 'SELECT a.id,'
			. ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
			. ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug'
			. ' FROM #__content AS a'
			. ' LEFT JOIN #__categories AS cc ON cc.id = a.catid';
			$institucionalParam = '';
			if ($byCategory) {
				$query .=  ' WHERE a.catid = ' . (int) $article->catid;
			} else {
				$query .=  ' WHERE a.sectionid = ' . (int) $article->sectionid;
				if ($institucional) {
					$query .= ' AND a.es_institucional = 1 ';
					#$orderby = 'cc.ordering, '.$orderby;
					$institucionalParam = '&institucionales=1';
				}
			}
			$query .= ' AND a.state = '. (int) $article->state
			. ($canPublish ? '' : ' AND a.access <= ' .(int) $user->get('aid', 0))
			. $xwhere
			. ' ORDER BY '. $orderby;
			$db->setQuery($query);
			$list = $db->loadObjectList('id');

			// this check needed if incorrect Itemid is given resulting in an incorrect result
			if ( !is_array($list) ) {
				$list = array();
			}

			reset($list);

			// location of current content item in array list
			$location = array_search($uid, array_keys($list));

			$rows = array_values($list);

			$article->prev = null;
			$article->next = null;

			if ($location -1 >= 0) 	{
				// the previous content item cannot be in the array position -1
				if (!$byCategory) {
					$article->prev = JRoute::_(ContentHelperRoute::getArticleRoute($rows[$location - 1]->slug)).$institucionalParam;
				} else {
					$article->prev = JRoute::_(ContentHelperRoute::getArticleRoute($rows[$location - 1]->slug, $article->catid));
				}

			}

			if (($location +1) < count($rows)) {
				// the next content item cannot be in an array position greater than the number of array postions
				if (!$byCategory) {
					$article->next = JRoute::_(ContentHelperRoute::getArticleRoute($rows[$location + 1]->slug)).$institucionalParam;
				} else {
					$article->next = JRoute::_(ContentHelperRoute::getArticleRoute($rows[$location + 1]->slug, $article->catid));
				}
			}


			$pnSpace = "";
			if (JText::_('&lt') || JText::_('&gt')) {
				$pnSpace = " ";
			}


		
			$html = '
			<table align="center" class="pagenav">
			<tr>'
			;
			if ($article->prev)
			{
				$html .= '
				<th class="pagenav_prev">
					<a href="'. $article->prev .'">'
						. JText::_( '&lt' ) . $pnSpace . JText::_( 'Siguiente' ) . '</a>
				</th>'
				;
			}

			if ($article->prev && $article->next)
			{
				$html .= '
				<td width="50">
					&nbsp;
				</td>'
				;
			}

			if ($article->next)
			{
				$html .= '
				<th class="pagenav_next">
					<a href="'. $article->next .'">'
						. JText::_( 'Anterior' ) . $pnSpace . JText::_( '&gt' ) .'</a>
				</th>'
				;
				
			}
			$html .= '
			</tr>
			</table>'
			;
		}

		return $html;
	}

}
